//
//  ModelBanner.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/12.
//

#import "ModelBanner.h"

NSString *const kModelBannerItemOrder     = KEY_order;
NSString *const kModelBannerItemBannerUrl = KEY_bannerUrl;
NSString *const kModelBannerItemJumpType  = KEY_jumpType;
NSString *const kModelBannerItemJumpUrl   = KEY_jumpUrl;


@interface ModelBanner ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ModelBanner

@synthesize order     = _order;
@synthesize bannerUrl = _bannerUrl;
@synthesize jumpType  = _jumpType;
@synthesize jumpUrl   = _jumpUrl;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
        self.order     = [[self objectOrNilForKey:kModelBannerItemOrder fromDictionary:dict] integerValue];
        self.bannerUrl = [self objectOrNilForKey:kModelBannerItemBannerUrl fromDictionary:dict];
        self.jumpType  = [[self objectOrNilForKey:kModelBannerItemJumpType fromDictionary:dict] integerValue];
        self.jumpUrl   = [self objectOrNilForKey:kModelBannerItemJumpUrl fromDictionary:dict];
    }
    return self;
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[NSNumber numberWithInteger:self.order] forKey:kModelBannerItemOrder];
    [mutableDict setValue:self.bannerUrl forKey:kModelBannerItemBannerUrl];
    [mutableDict setValue:[NSNumber numberWithInteger:self.jumpType] forKey:kModelBannerItemJumpType];
    [mutableDict setValue:self.jumpUrl forKey:kModelBannerItemJumpUrl];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}

@end
